package com.fulihui.information.service;

import com.alibaba.fastjson.JSON;
import com.fulihui.information.api.DataProcessService;
import com.fulihui.information.core.zbus.ZbusProducerHandle;
import com.fulihui.information.request.SpiderDataUploadRequest;
import com.fulihui.information.result.SpiderDataUploadResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.error.Errors;
import org.near.servicesupport.result.ResultBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

/**
 * Created by LeeSon on 2016/12/25 0025.
 */
@Service(version = "1.0.0")
@Slf4j
public class DataProcessServiceImpl implements DataProcessService {
    @Autowired
    private ZbusProducerHandle zbusProducerHandle;

    @Override
    public SpiderDataUploadResponse spiderDataUpload(SpiderDataUploadRequest request) {

        //生成业务跟踪编号
        request.setTrackNo(String.valueOf(UUID.randomUUID()));

        String messageJson = JSON.toJSONString(request);
        log.info("C->S: " + messageJson);
        boolean success = zbusProducerHandle.commitToMQ(messageJson);

        return success ? ResultBuilder.succ(SpiderDataUploadResponse.class)
                : ResultBuilder.fail(SpiderDataUploadResponse.class, Errors.Commons.SYSTEM_ERROR);
    }
}